package io.renren.modules.product.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.renren.common.utils.R;
import io.renren.modules.product.entity.ProductEntity;
import io.renren.modules.product.service.ProductService;
import io.renren.modules.product.vo.ProductCeateVo;
import io.renren.modules.product.vo.ProductUpdateVo;
import io.renren.modules.product.vo.ProductVo;
import io.renren.modules.sys.controller.AbstractController;
import io.swagger.annotations.*;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhaoyu
 * @since 2023-07-29
 */
@RestController
@RequestMapping("/product")
@Api(tags = "产品管理")
public class ProductController extends AbstractController {


    @Autowired
    private ProductService productService;


    @GetMapping(value = "/list")
    @ApiOperation("获取产品列表信息")
    @RequiresPermissions("product:list")
    @ApiImplicitParam(value = "鉴权token",name = "token",paramType = "header", dataType = "String", required=true)
    public R list(
            @RequestParam(required = false) Integer current,
            @RequestParam(required = false) Integer pageSize,
            @RequestParam(required = false) String productName) {
        if (current == null) {
            current = 1;
        }
        if (pageSize == null) {
            pageSize = 10;
        }
        Page<ProductVo> page =  productService.getList(current,pageSize,productName,getUserId());
        return R.ok().put("page",page);
    }

    @GetMapping(value = "/getOneProduct/{id}")
    @ApiOperation("根据id获取某一产品")
    @RequiresPermissions("product:getOneProduct")
    @ApiImplicitParam(value = "鉴权token",name = "token",paramType = "header", dataType = "String", required=true)
    public R getOneProduct(@PathVariable("id") @ApiParam("产品id") Long id) {
        if (null == id)return R.error("产品id不能为空");
        ProductEntity product = productService.getById(id);
        return R.ok("成功").put("product",product);
    }

    @PostMapping(value = "/create")
    @ApiOperation("创建产品")
    @RequiresPermissions("product:createProduct")
    @ApiImplicitParam(value = "鉴权token",name = "token",paramType = "header", dataType = "String", required=true)
    public R createProduct(@RequestBody ProductCeateVo productCeateVo) {
        productService.createProduct(productCeateVo);
        return R.ok("created successfully");
    }

    @GetMapping(value = "/deleteProductById/{id}")
    @ApiOperation("根据产品id删除产品")
    @RequiresPermissions("product:delete")
    @ApiImplicitParam(value = "鉴权token",name = "token",paramType = "header", dataType = "String", required=true)
    public R deleteProductById(@PathVariable("id") Long id) {
        if (null == id)return R.error("产品id不能为空");
        productService.deleteProductById(id);
        return R.ok("deleted successfully");
    }

    @PostMapping(value = "/update")
    @ApiOperation("更新产品信息")
    @RequiresPermissions("product:update")
    @ApiImplicitParam(value = "鉴权token",name = "token",paramType = "header", dataType = "String", required=true)
    public R update(@RequestBody ProductUpdateVo productUpdateVo) {
        if (null == productUpdateVo.getProductId())return R.error("产品id不能为空");
        productService.updateProductById(productUpdateVo);
        return R.ok("updated successfully");
    }

    /**
     * 显示所有产品信息
     *
     * @return 返回所有产品信息
     */
    @GetMapping("/allProduct")
    @ApiOperation("获取所有产品信息")
    @ApiImplicitParam(value = "鉴权token",name = "token",paramType = "header", dataType = "String", required=true)
    @RequiresPermissions("product:allProduct")
    public R getAllProduct(){
        List<ProductEntity> list = productService.list();
        return R.ok().put("data",list);
    }


}
